package com.ygqh.baby.controller.mobile;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.model.YgShoppingCarModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgShoppingCarService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车相关
 *
 * @author guohao
 */
@Controller
@RequestMapping("/app/shoppingcar")
public class YgShoppingCarController extends BaseController {

	@Autowired
	private YgShoppingCarService shoppingCarService;
	@Autowired
	private DocumentConstant documentConstant;

	/**
	 * 获取购物车列表
	 *
	 * @param queryInfo 分页
	 * @return
	 */
	@RequestMapping(value = "findShoppingCarList")
	@ResponseBody
	public ResponseEntity findShoppingCarList(QueryInfo queryInfo) {
		YgUser user = SessionUtil.getCurrentUser();
		List<YgShoppingCarModel> list = shoppingCarService.findShoppingCarList(queryInfo, user.getId(),
				false, SALES_PLATFORM);
		List<List<YgShoppingCarModel>> carList = shoppingCarService.sortCarListV2(list, user.getId());
		return ResponseEntity.success(carList);
	}

	/**
	 * 添加普通商品到购物车
	 *
	 * @param skuId    sku id
	 * @param quantity 数量
	 * @return
	 */
	@RequestMapping(value = "addOrdinaryItemNew")
	@ResponseBody
	public ResponseEntity addOrdinaryItemNew(Long skuId, Long quantity) {
		YgUser user = SessionUtil.getCurrentUser();
		ResponseEntity msg;
		if (quantity == null || quantity <= 0) {
			return ResponseEntity.error("请选择购买数量。");
		}
		int row = shoppingCarService.addOrdinaryItem(skuId, quantity, user.getId(), SALES_PLATFORM);
		if (row > 0) {
			msg = ResponseEntity.success(row);
		} else {
			msg = ResponseEntity.error("操作失败，库存不足！");
		}
		return msg;
	}

	/**
	 * 添加 加价购或满赠商品到购物车
	 *
	 * @param skuId          skuId
	 * @param pType          促销类型
	 * @param promotionPrice 促销价格
	 * @return
	 */
	@RequestMapping(value = "addPromotionItem")
	@ResponseBody
	public ResponseEntity addPromotionItem(Long skuId, PromotionType pType, BigDecimal promotionPrice) {
		YgUser user = SessionUtil.getCurrentUser();
		Message msg = shoppingCarService.addPromotionItem(skuId, 1L, user.getId(), pType, promotionPrice,
				SALES_PLATFORM);
		if (msg.getType().equals(Message.Type.success)) {
			YgShoppingCarModel carModel = shoppingCarService.findShoppingCarById((Long) msg.getExtra());
			msg.setExtra(carModel);
		}
		return ResponseEntity.converter(msg);
	}

	/**
	 * 修改商品数量
	 *
	 * @param skuId skuId
	 * @param isAdd 是否增加
	 * @return
	 */
	@RequestMapping(value = "updateNum")
	@ResponseBody
	public ResponseEntity updateNum(Long skuId, Boolean isAdd) {
		ResponseEntity msg;
		YgUser user = SessionUtil.getCurrentUser();
		int row = shoppingCarService.updateNum(skuId, isAdd, user.getId(), SALES_PLATFORM);
		if (row > 0) {
			msg = ResponseEntity.success(row);
		} else {
			msg = ResponseEntity.error(row);
		}
		return msg;
	}

	/**
	 * 获取购物车中商品总数量
	 *
	 * @return
	 */
	@RequestMapping(value = "findCount")
	@ResponseBody
	public ResponseEntity findItemCount() {
		YgUser user = SessionUtil.getCurrentUser();
		int count = shoppingCarService.findItemCount(user.getId(), SALES_PLATFORM);
		return ResponseEntity.success(count);
	}

	/**
	 * 删除 购物车中商品
	 *
	 * @param carId 购物车id
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public ResponseEntity deleteShoppingCar(Long carId) {
		YgUser user = SessionUtil.getCurrentUser();
		int row = shoppingCarService.deleteSoft(carId, user.getId());
		ResponseEntity msg;
		if (row > 0) {
			msg = ResponseEntity.success(null);
		} else {
			msg = ResponseEntity.error(null);
		}
		return msg;
	}

	/**
	 * 清空购物车
	 *
	 * @return
	 */
	@RequestMapping("removeShoppingCar")
	@ResponseBody
	public ResponseEntity removeShoppingCar() {
		YgUser user = SessionUtil.getCurrentUser();
		ResponseEntity msg;
		int row = shoppingCarService.removeShoppingCar(user.getId(), SALES_PLATFORM);
		if (row > 0) {
			msg = ResponseEntity.success(null);
		} else {
			msg = ResponseEntity.error(documentConstant.MESSAGE_ERROR);
		}
		return msg;
	}
}
